package com.hxh.wechat.activity

import android.content.Context
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.billy.android.swipe.SmartSwipe
import com.billy.android.swipe.consumer.SpaceConsumer
import com.billy.android.swipe.consumer.StretchConsumer
import com.google.gson.JsonObject
import com.hxh.wechat.R
import com.hxh.wechat.adpter.ContactAdapter
import com.hxh.wechat.api.UserApi
import com.hxh.wechat.model.ContactFriend
import com.hxh.wechat.model.Star
import com.hxh.wechat.util.DBUtil
import okhttp3.Call
import okhttp3.Callback
import okhttp3.Response
import java.io.IOException


class ContactActivity : Fragment() {
    companion object {
        val contactList = ArrayList<ContactAdapter.ContactItem>()

        private lateinit var recyclerView: RecyclerView
        fun notifyDataSetChanged() {
            Handler(Looper.getMainLooper()).post {
                recyclerView.adapter?.notifyDataSetChanged()
            }
        }
    }

    lateinit var contactAdapter: ContactAdapter
    val handler = Handler()
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        val view = inflater.inflate(R.layout.contacts_page_layout, container, false)
        recyclerView = view.findViewById<RecyclerView>(R.id.contactList)
        recyclerView.layoutManager = LinearLayoutManager(context)
        contactAdapter = ContactAdapter(contactList)
        recyclerView.adapter = contactAdapter
        SmartSwipe.wrap(recyclerView).addConsumer<StretchConsumer>(StretchConsumer())
            .enableVertical()
        initStar(view.context)
        return view
    }

    private fun initStar(context: Context) {
        contactList.clear();
        var user = DBUtil.getUser(context);
        UserApi.myFriends(user!!.id)?.enqueue(object : Callback {
            override fun onFailure(call: Call, e: IOException) {
                println("Failed.")
                println(e.stackTrace.toString())
            }

            @Throws(IOException::class)
            override fun onResponse(call: Call, response: Response) {
                val responseData = response.body?.string()
                var contactListResult =
                    DBUtil.getGson().fromJson(responseData, JsonObject::class.java)
                        .getAsJsonArray("data");
                for (element in contactListResult) {
                    var contactFriend =
                        DBUtil.getGson().fromJson(element, ContactFriend::class.java)
                    contactList.add(
                        ContactAdapter.ContactItem(
                            contactFriend.friendUserId,
                            contactFriend.friendFaceImage,
                            contactFriend.friendUsername,
                            contactFriend.friendNickname,
                        )
                    );
                }
                handler.post {
                    contactAdapter.notifyDataSetChanged()
                }

            }
        })
    }
}